import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(process.cwd(), 'src'),
    },
  },
  // 指定静态资源目录
  publicDir: 'public',
  // 解决"process is not defined"错误
  define: {
    'process.env': process.env,
    // 提供一个空的process对象，以防代码中直接使用process
    'process': {
      env: {}
    }
  },
  // 构建配置
  build: {
    assetsDir: 'assets',
    outDir: 'dist',
    // 清空构建目录
    emptyOutDir: true,
    // 提升构建性能
    chunkSizeWarningLimit: 2000,
  },
  server: {
    port: 5173,
    open: true,
    // 启用HMR并添加覆盖层
    hmr: {
      overlay: true,
    },
    proxy: {
      // 管理后台API前缀
      '/adminapi': {
        target: 'http://localhost:5000', // 修改为正确的后端端口5000
        changeOrigin: true,
        secure: false,
        // 确保Cookie能够正确传递
        cookieDomainRewrite: 'localhost',
        // 配置请求头
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
          'Access-Control-Allow-Credentials': 'true'
        }
      },
      // 前台网站API前缀
      '/webapi': {
        target: 'http://localhost:5000', // 修改为正确的后端端口5000
        changeOrigin: true,
        secure: false,
        // 确保Cookie能够正确传递
        cookieDomainRewrite: 'localhost',
        // 配置请求头
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
          'Access-Control-Allow-Credentials': 'true'
        }
      }
    },
  },
})